﻿using System;
using System.Collections.Generic;

namespace Battle.Engine
{
    public class FPosition
    {
        public const int COL = 5;
        public const int ROW = 3;
        public const int FRONT_ROW = 1;
        public const int MIDDLE_ROW = 2;
        public const int BACK_ROW = 3;

        public int col;         //列
        public int row;         //行
        public CampType camp;   //进攻or防守

        public FPosition(int fpid, CampType camp)
        {
            this.row = (fpid - 1) / COL + 1;
            this.col = fpid - (row - 1) * COL;
            this.camp = camp;
        }

        public FPosition(int col, int row, CampType camp)
        {
            this.row = row;
            this.col = col;
            this.camp = camp;
        }

        public void SetFPID(int newFPId) {
            this.row = (newFPId - 1) / COL + 1;
            this.col = newFPId - (row - 1) * COL;
        }

        public int ToFPID()
        {
            return col + (row - 1) * COL;
        }

        public static bool operator ==(FPosition lhs, FPosition rhs)
        {
            if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null))
            {
                return true;
            }
            else if (!ReferenceEquals(lhs, null) && !ReferenceEquals(rhs, null))
            {
                return lhs.col == rhs.col && lhs.row == rhs.row && lhs.camp == rhs.camp;
            }
            else {
                return false;
            }
        }

        public static bool operator !=(FPosition lhs, FPosition rhs)
        {
            if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null))
            {
                return false;
            }
            else if (!ReferenceEquals(lhs, null) && !ReferenceEquals(rhs, null))
            {
                return !(lhs.col == rhs.col && lhs.row == rhs.row && lhs.camp == rhs.camp);
            }
            else {
                return true;
            }
        }

        public override bool Equals(object obj)
        {
            FPosition fp = (FPosition)obj;
            if(fp == null)
            {
                return false;
            }
            return this == fp;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
